﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Battle.Engine.LogicModel
{
    public class FBuffProc
    {
        public FActor actor;
        public EngineData engineData;
        public FBuffProc(FActor actor)
        {
            this.actor = actor;
            this.engineData = actor.player.engineData;
        }

        public void LoadAttBuff(FSkillCast cast, FExportOnceData exportOnceData)
        {
            //处理技能带的非技能限定buff
            foreach (var skillBuff in cast.skillBuffs)
            {
                if (skillBuff.buffHitClass == BUFFHitClass.INDEPENDENT)
                {
                    //buff目标获取的限制条件
                    List<SelectLimitCondition> conditions;
                    if (skillBuff.buffTrigger.buffTemplate.isDebuff)
                    {//无敌不受debuff
                        conditions = this.engineData.LimitConditions(true, true, false);
                    }
                    else
                    {
                        conditions = this.engineData.LimitConditions(true, true, true);
                    }
                    List<FActor> targetActors = new List<FActor>();
                    if ((SelectStrategy)skillBuff.buffTrigger.buffSelect == SelectStrategy.SAME_AS_CAST)
                    {
                        foreach(var target in cast.skill.castTargets)
                        {
                            if (this.engineData.IsActorMatchCondition(target, cast.skill.target, conditions))
                                targetActors.Add(target);
                        }
                    }
                    else
                    {
                        targetActors.AddRange(this.engineData.SelectTargetActors(false, skillBuff.buffTrigger.buffSelect, this.actor, cast.skill.target, conditions));
                    }
                    foreach (var ta in targetActors)
                    {
                        FActor conditonTarget = skillBuff.buffTrigger.buffSelect == 0 ? cast.skill.target : ta;
                        //判断buff条件是否生效
                        if (skillBuff.triggerCondition.IsConditionTure(conditonTarget))
                        {
                            ta.buffManager.LoadSkillBuff(cast, skillBuff.buffTrigger, TalentBuffTriggerTimer.AllAtt, exportOnceData);
                        }
                    }
                }
            }
            foreach (var talent in this.actor.talents)
            {
                foreach (var talentBuff in talent.talentBuffs)
                {
                    //buff目标获取的限制条件
                    List<SelectLimitCondition> conditions;
                    if (talentBuff.buffTrigger.buffTemplate.isDebuff)
                    {//无敌不受debuff
                        conditions = this.engineData.LimitConditions(true, true, false);
                    }
                    else
                    {
                        conditions = this.engineData.LimitConditions(true, true, true);
                    }
                    if (cast.skill.IsUltimate)
                    {
                        if (talentBuff.triggerTimer == TalentBuffTriggerTimer.AllAtt
                            || talentBuff.triggerTimer == TalentBuffTriggerTimer.UltimateAtt)
                        {
                            List<FActor> targetActors = new List<FActor>();
                            if ((SelectStrategy)talentBuff.buffTrigger.buffSelect == SelectStrategy.SAME_AS_CAST)
                            {
                                foreach (var target in cast.skill.castTargets)
                                {
                                    if (this.engineData.IsActorMatchCondition(target, cast.skill.target, conditions))
                                        targetActors.Add(target);
                                }
                            }
                            else
                            {
                                targetActors.AddRange(this.engineData.SelectTargetActors(false, talentBuff.buffTrigger.buffSelect, this.actor, cast.skill.target, conditions));
                            }
                            foreach (var ta in targetActors)
                            {
                                FActor conditonTarget = talentBuff.buffTrigger.buffSelect == 0 ? cast.skill.target : ta;
                                //判断天赋buff条件是否生效
                                if (talentBuff.triggerCondition.IsConditionTure(conditonTarget))
                                {
                                    ta.buffManager.LoadTalentBuff(talent, talentBuff, TalentBuffTriggerTimer.AllAtt, exportOnceData);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (talentBuff.triggerTimer == TalentBuffTriggerTimer.AllAtt
                               || talentBuff.triggerTimer == TalentBuffTriggerTimer.NormalAtt)
                        {
                            List<FActor> targetActors = new List<FActor>();
                            if ((SelectStrategy)talentBuff.buffTrigger.buffSelect == SelectStrategy.SAME_AS_CAST)
                            {
                                foreach (var target in cast.skill.castTargets)
                                {
                                    if (this.engineData.IsActorMatchCondition(target, cast.skill.target, conditions))
                                        targetActors.Add(target);
                                }
                            }
                            else
                            {
                                targetActors.AddRange(this.engineData.SelectTargetActors(false, talentBuff.buffTrigger.buffSelect, this.actor, cast.skill.target, conditions));
                            }
                            foreach (var ta in targetActors)
                            {
                                FActor conditonTarget = talentBuff.buffTrigger.buffSelect == 0 ? cast.skill.target : ta;
                                //判断天赋buff条件是否生效
                                if (talentBuff.triggerCondition.IsConditionTure(conditonTarget))
                                {
                                    ta.buffManager.LoadTalentBuff(talent, talentBuff, TalentBuffTriggerTimer.AllAtt, exportOnceData);
                                }
                            }
                        }
                    }
                }
            }
        }


        public void LoadAttEndBuff(FSkillCast cast, FExportOnceData exportOnceData)
        {
            //处理技能带的非技能限定buff
            foreach (var skillBuff in cast.skillBuffs)
            {
                if (skillBuff.buffHitClass == BUFFHitClass.SKILLENDBUFF)
                {
                    //buff目标获取的限制条件
                    List<SelectLimitCondition> conditions;
                    if (skillBuff.buffTrigger.buffTemplate.isDebuff)
                    {//无敌不受debuff
                        conditions = this.engineData.LimitConditions(true, true, false);
                    }
                    else
                    {
                        conditions = this.engineData.LimitConditions(true, true, true);
                    }
                    List<FActor> targetActors = this.engineData.SelectTargetActors(false, skillBuff.buffTrigger.buffSelect, this.actor, cast.skill.target, conditions);
                    foreach (var ta in targetActors)
                    {
                        FActor conditonTarget = skillBuff.buffTrigger.buffSelect == 0 ? cast.skill.target : ta;
                        //判断buff条件是否生效
                        if (skillBuff.triggerCondition.IsConditionTure(conditonTarget))
                        {
                            ta.buffManager.LoadSkillBuff(cast, skillBuff.buffTrigger, TalentBuffTriggerTimer.AllAttEnd, exportOnceData);
                        }
                    }
                }
            }
            foreach (var talent in this.actor.talents)
            {
                foreach (var talentBuff in talent.talentBuffs)
                {
                    //buff目标获取的限制条件
                    List<SelectLimitCondition> conditions;
                    if (talentBuff.buffTrigger.buffTemplate.isDebuff)
                    {//无敌不受debuff
                        conditions = this.engineData.LimitConditions(true, true, false);
                    }
                    else
                    {
                        conditions = this.engineData.LimitConditions(true, true, true);
                    }
                    if (cast.skill.IsUltimate)
                    {
                        if (talentBuff.triggerTimer == TalentBuffTriggerTimer.AllAttEnd
                            || talentBuff.triggerTimer == TalentBuffTriggerTimer.UltimateAttEnd)
                        {
                            List<FActor> targetActors = this.engineData.SelectTargetActors(false, talentBuff.buffTrigger.buffSelect, this.actor, cast.skill.target, conditions);
                            foreach (var ta in targetActors)
                            {
                                FActor conditonTarget = talentBuff.buffTrigger.buffSelect == 0 ? cast.skill.target : ta;
                                //判断天赋buff条件是否生效
                                if (talentBuff.triggerCondition.IsConditionTure(conditonTarget))
                                {
                                    ta.buffManager.LoadTalentBuff(talent, talentBuff, TalentBuffTriggerTimer.AllAttEnd, exportOnceData);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (talentBuff.triggerTimer == TalentBuffTriggerTimer.AllAttEnd
                               || talentBuff.triggerTimer == TalentBuffTriggerTimer.NormalAttEnd)
                        {
                            List<FActor> targetActors = this.engineData.SelectTargetActors(false, talentBuff.buffTrigger.buffSelect, this.actor, cast.skill.target, conditions);
                            foreach (var ta in targetActors)
                            {
                                FActor conditonTarget = talentBuff.buffTrigger.buffSelect == 0 ? cast.skill.target : ta;
                                //判断天赋buff条件是否生效
                                if (talentBuff.triggerCondition.IsConditionTure(conditonTarget))
                                {
                                    ta.buffManager.LoadTalentBuff(talent, talentBuff, TalentBuffTriggerTimer.AllAttEnd, exportOnceData);
                                }
                            }
                        }
                    }
                }
            }
        }

        public void LoadHitBuff(FSkillCast cast, FActor target, FExportOnceData exportOnceData)
        {
            //处理技能带的技能限定buff
            foreach (var skillBuff in cast.skillBuffs)
            {
                if (skillBuff.buffHitClass == BUFFHitClass.SKILLBUFF)
                {
                    //buff目标获取的限制条件
                    List<SelectLimitCondition> conditions;
                    if (skillBuff.buffTrigger.buffTemplate.isDebuff)
                    {//无敌不受debuff
                        conditions = this.engineData.LimitConditions(true, true, false);
                    }
                    else
                    {
                        conditions = this.engineData.LimitConditions(true, true, true);
                    }
                    List<FActor> targetActors = this.engineData.SelectTargetActors(false, skillBuff.buffTrigger.buffSelect, this.actor, target, conditions);
                    foreach (var ta in targetActors)
                    {
                        FActor conditonTarget = skillBuff.buffTrigger.buffSelect == 0 ? target : ta;
                        //判断buff条件是否生效
                        if (skillBuff.triggerCondition.IsConditionTure(conditonTarget))
                        {
                            ta.buffManager.LoadSkillBuff(cast, skillBuff.buffTrigger, TalentBuffTriggerTimer.Hit, exportOnceData);
                        }
                    }
                }
            }
            foreach (var talent in this.actor.talents)
            {
                foreach (var talentBuff in talent.talentBuffs)
                {
                    if (talentBuff.triggerTimer == TalentBuffTriggerTimer.Hit)
                    {
                        //buff目标获取的限制条件
                        List<SelectLimitCondition> conditions;
                        if (talentBuff.buffTrigger.buffTemplate.isDebuff)
                        {//无敌不受debuff
                            conditions = this.engineData.LimitConditions(true, true, false);
                        }
                        else
                        {
                            conditions = this.engineData.LimitConditions(true, true, true);
                        }
                        //获取buff作用目标
                        List<FActor> targetActors = this.engineData.SelectTargetActors(false, talentBuff.buffTrigger.buffSelect, this.actor, target, conditions);
                        foreach (var ta in targetActors)
                        {
                            FActor conditonTarget = talentBuff.buffTrigger.buffSelect == 0 ? target : ta;
                            //判断天赋buff条件是否生效
                            if (talentBuff.triggerCondition.IsConditionTure(conditonTarget))
                            {
                                ta.buffManager.LoadTalentBuff(talent, talentBuff, TalentBuffTriggerTimer.Hit, exportOnceData);
                            }
                        }
                    }
                }
            }
        }

        public void LoadBeHitBuff(FSkillCast cast, FExportOnceData exportOnceData)
        {
            //buff目标获取的限制条件
            List<SelectLimitCondition> conditions = this.engineData.LimitConditions(true, true, true);
            foreach (var talent in this.actor.talents)
            {
                foreach (var talentBuff in talent.talentBuffs)
                {
                    if (cast.exportType == AttackType.TREAT)
                    {
                        if(talentBuff.triggerTimer == TalentBuffTriggerTimer.Health)
                        {
                            //获取buff作用目标
                            List<FActor> targetActors = new List<FActor>();
                            targetActors.AddRange(this.engineData.SelectTargetActors(false, talentBuff.buffTrigger.buffSelect, this.actor, cast.skill.actor, conditions));

                            foreach (var ta in targetActors)
                            {
                                FActor conditonTarget = talentBuff.buffTrigger.buffSelect == 0 ? cast.skill.actor : ta;
                                //判断天赋buff条件是否生效
                                if (talentBuff.triggerCondition.IsConditionTure(conditonTarget))
                                {
                                    ta.buffManager.LoadTalentBuff(talent, talentBuff, TalentBuffTriggerTimer.Health, exportOnceData);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (cast.skill.IsUltimate)
                        {
                            if (talentBuff.triggerTimer == TalentBuffTriggerTimer.AllHurt
                                || talentBuff.triggerTimer == TalentBuffTriggerTimer.UltimateHurt)
                            {
                                //获取buff作用目标
                                List<FActor> targetActors = new List<FActor>();
                                targetActors.AddRange(this.engineData.SelectTargetActors(false, talentBuff.buffTrigger.buffSelect, this.actor, cast.skill.actor, conditions));

                                foreach (var ta in targetActors)
                                {
                                    FActor conditonTarget = talentBuff.buffTrigger.buffSelect == 0 ? cast.skill.actor : ta;
                                    //判断天赋buff条件是否生效
                                    if (talentBuff.triggerCondition.IsConditionTure(conditonTarget))
                                    {
                                        ta.buffManager.LoadTalentBuff(talent, talentBuff, TalentBuffTriggerTimer.AllHurt, exportOnceData);
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (talentBuff.triggerTimer == TalentBuffTriggerTimer.AllHurt
                                   || talentBuff.triggerTimer == TalentBuffTriggerTimer.NormalHurt)
                            {
                                //获取buff作用目标
                                List<FActor> targetActors = new List<FActor>();
                                targetActors.AddRange(this.engineData.SelectTargetActors(false, talentBuff.buffTrigger.buffSelect, this.actor, cast.skill.actor, conditions));

                                foreach (var ta in targetActors)
                                {
                                    FActor conditonTarget = talentBuff.buffTrigger.buffSelect == 0 ? cast.skill.actor : ta;
                                    //判断天赋buff条件是否生效
                                    if (talentBuff.triggerCondition.IsConditionTure(conditonTarget))
                                    {
                                        ta.buffManager.LoadTalentBuff(talent, talentBuff, TalentBuffTriggerTimer.AllHurt, exportOnceData);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        //闪避时 被暴击时 格挡时 被技能选中时
        public void LoadHurtBuffByTimer(FSkillCast cast, TalentBuffTriggerTimer timer, FExportOnceData exportOnceData)
        {
            foreach (var talent in this.actor.talents)
            {
                foreach (var talentBuff in talent.talentBuffs)
                {
                    if (talentBuff.triggerTimer == timer)
                    {
                        //buff目标获取的限制条件
                        List<SelectLimitCondition> conditions = this.engineData.LimitConditions(true, true, true);
                        //获取buff作用目标
                        List<FActor> targetActors = this.engineData.SelectTargetActors(false, talentBuff.buffTrigger.buffSelect, this.actor, cast.skill.actor, conditions);
                        foreach (var ta in targetActors)
                        {
                            FActor conditonTarget = talentBuff.buffTrigger.buffSelect == 0 ? cast.skill.actor : ta;
                            //判断天赋buff条件是否生效
                            if (talentBuff.triggerCondition.IsConditionTure(conditonTarget))
                            {
                                ta.buffManager.LoadTalentBuff(talent, talentBuff, timer, exportOnceData);
                            }
                        }
                    }
                }
            }
            if (timer == TalentBuffTriggerTimer.BeCri)
            {//被暴击时 要处理队友被暴击时的buff
                foreach (var teammateActor in this.actor.player.actors)
                {
                    if (teammateActor.state == FActorState.DEAD || teammateActor.uniqueId.Equals(this.actor.uniqueId))
                        continue;
                    foreach (var talent in teammateActor.talents)
                    {
                        foreach (var talentBuff in talent.talentBuffs)
                        {
                            if (talentBuff.triggerTimer == TalentBuffTriggerTimer.TeammateBeCri)
                            {
                                //buff目标获取的限制条件
                                List<SelectLimitCondition> conditions = this.engineData.LimitConditions(true, true, true);
                                //获取buff作用目标
                                List<FActor> targetActors = this.engineData.SelectTargetActors(false, talentBuff.buffTrigger.buffSelect, this.actor, cast.skill.actor, conditions);
                                foreach (var ta in targetActors)
                                {
                                    FActor conditonTarget = talentBuff.buffTrigger.buffSelect == 0 ? cast.skill.actor : ta;
                                    //判断天赋buff条件是否生效
                                    if (talentBuff.triggerCondition.IsConditionTure(conditonTarget))
                                    {
                                        ta.buffManager.LoadTalentBuff(talent, talentBuff, TalentBuffTriggerTimer.TeammateBeCri, exportOnceData);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 被浮空倒地时
        /// </summary>
        /// <param name="timer">TalentBuffTriggerTimer.BeFloat、BeFall</param>
        public void LoadBeFloatOrFallBuff(FSkillCast cast, TalentBuffTriggerTimer timer, FExportOnceData exportOnceData)
        {
            
            foreach (var talent in this.actor.talents)
            {
                foreach (var talentBuff in talent.talentBuffs)
                {
                    if (talentBuff.triggerTimer == timer)
                    {
                        //buff目标获取的限制条件
                        List<SelectLimitCondition> conditions = this.engineData.LimitConditions(true, true, true);
                        //获取buff作用目标
                        List<FActor> targetActors = this.engineData.SelectTargetActors(false, talentBuff.buffTrigger.buffSelect, this.actor, cast.skill.actor, conditions);
                        foreach (var ta in targetActors)
                        {
                            FActor conditonTarget = talentBuff.buffTrigger.buffSelect == 0 ? cast.skill.actor : ta;
                            //判断天赋buff条件是否生效
                            if (talentBuff.triggerCondition.IsConditionTure(conditonTarget))
                            {
                                ta.buffManager.LoadTalentBuff(talent, talentBuff, timer, exportOnceData);
                            }
                        }
                    }
                }
            }
        }

        public void LoadCriBuff(FSkillCast cast, FActor target, FExportOnceData exportOnceData)
        {//处理技能带的技能限定buff
            foreach (var skillBuff in cast.skillBuffs)
            {
                if (skillBuff.buffHitClass == BUFFHitClass.SKILLCRIBUFF)
                {
                    //buff目标获取的限制条件
                    List<SelectLimitCondition> conditions;
                    if (skillBuff.buffTrigger.buffTemplate.isDebuff)
                    {//无敌不受debuff
                        conditions = this.engineData.LimitConditions(true, true, false);
                    }
                    else
                    {
                        conditions = this.engineData.LimitConditions(true, true, true);
                    }
                    List<FActor> targetActors = this.engineData.SelectTargetActors(false, skillBuff.buffTrigger.buffSelect, this.actor, target, conditions);
                    foreach (var ta in targetActors)
                    {
                        FActor conditonTarget = skillBuff.buffTrigger.buffSelect == 0 ? target : ta;
                        //判断buff条件是否生效
                        if (skillBuff.triggerCondition.IsConditionTure(conditonTarget))
                        {
                            ta.buffManager.LoadSkillBuff(cast, skillBuff.buffTrigger, TalentBuffTriggerTimer.Cri, exportOnceData);
                        }
                    }
                }
            }
            foreach (var talent in this.actor.talents)
            {
                foreach (var talentBuff in talent.talentBuffs)
                {
                    if (talentBuff.triggerTimer == TalentBuffTriggerTimer.Cri)
                    {
                        //buff目标获取的限制条件
                        List<SelectLimitCondition> conditions;
                        if (talentBuff.buffTrigger.buffTemplate.isDebuff)
                        {//无敌不受debuff
                            conditions = this.engineData.LimitConditions(true, true, false);
                        }
                        else
                        {
                            conditions = this.engineData.LimitConditions(true, true, true);
                        }
                        //获取buff作用目标
                        List<FActor> targetActors = this.engineData.SelectTargetActors(false, talentBuff.buffTrigger.buffSelect, this.actor, target, conditions);
                        foreach (var ta in targetActors)
                        {
                            FActor conditonTarget = talentBuff.buffTrigger.buffSelect == 0 ? target : ta;
                            //判断天赋buff条件是否生效
                            if (talentBuff.triggerCondition.IsConditionTure(conditonTarget))
                            {
                                ta.buffManager.LoadTalentBuff(talent, talentBuff, TalentBuffTriggerTimer.Cri, exportOnceData);
                            }
                        }
                    }
                }
            }

        }
        
        public void LoadKillBuff(FSkill skill)
        {
            foreach (var talent in this.actor.talents)
            {
                FExportOnceData exportOnceData = new FExportOnceData();
                exportOnceData.Init(talent);
                foreach (var talentBuff in talent.talentBuffs)
                {
                    if (talentBuff.triggerTimer == TalentBuffTriggerTimer.Kill)
                    {
                        //buff目标获取的限制条件
                        List<SelectLimitCondition> conditions;
                        if (talentBuff.buffTrigger.buffTemplate.isDebuff)
                        {//无敌不受debuff
                            conditions = this.engineData.LimitConditions(true, true, false);
                        }
                        else
                        {
                            conditions = this.engineData.LimitConditions(true, true, true);
                        }
                        //获取buff作用目标
                        List<FActor> targetActors = this.engineData.SelectTargetActors(false, talentBuff.buffTrigger.buffSelect, this.actor, skill.target, conditions);
                        foreach (var ta in targetActors)
                        {
                            FActor conditonTarget = talentBuff.buffTrigger.buffSelect == 0 ? skill.target : ta;
                            //判断天赋buff条件是否生效
                            if (talentBuff.triggerCondition.IsConditionTure(conditonTarget))
                            {
                                ta.buffManager.LoadTalentBuff(talent, talentBuff, TalentBuffTriggerTimer.Kill, exportOnceData);
                            }
                        }
                    }
                }
                if (exportOnceData.loadBuffs.Count > 0 || exportOnceData.unloadBuffs.Count > 0)
                {
                    this.engineData.exports.Add(exportOnceData);
                }
            }

        }

        public void LoadBuffToTargetByTimer(TalentBuffTriggerTimer timer, FActor target)
        {
            foreach (var talent in this.actor.talents)
            {
                FExportOnceData exportOnceData = new FExportOnceData();
                exportOnceData.Init(talent);
                foreach (var talentBuff in talent.talentBuffs)
                {
                    if (talentBuff.triggerTimer == timer)
                    {
                        //判断天赋buff条件是否生效
                        if (talentBuff.triggerCondition.IsConditionTure(target))
                        {
                            target.buffManager.LoadTalentBuff(talent, talentBuff, timer, exportOnceData);
                        }
                    }
                }
                if (exportOnceData.loadBuffs.Count > 0 || exportOnceData.unloadBuffs.Count > 0)
                {
                    this.engineData.exports.Add(exportOnceData);
                }
            }
        }

        /// <summary>
        /// 只战斗开始，回合开始，自己死亡，队友死亡进行调用,这四种状态找不到对应目标，涉及到目标的buff不可用
        /// </summary>
        /// <param name="timer">TalentBuffTriggerTimer.FightBegin、RoundBegin、Die、TeammateDie</param>
        public void LoadBuffByTimer(TalentBuffTriggerTimer timer)
        {
            foreach (var talent in this.actor.talents)
            {
                FExportOnceData exportOnceData = new FExportOnceData();
                exportOnceData.Init(talent);
                foreach (var talentBuff in talent.talentBuffs)
                {
                    if (talentBuff.triggerTimer == timer)
                    {
                        //buff目标获取的限制条件
                        List<SelectLimitCondition> conditions = this.engineData.LimitConditions(true, true, true);
                        //获取buff作用目标
                        List<FActor> targetActors = this.engineData.SelectTargetActors(false, talentBuff.buffTrigger.buffSelect, this.actor, null, conditions);
                        foreach (var ta in targetActors)
                        {
                            //判断天赋buff条件是否生效
                            if (talentBuff.triggerCondition.IsConditionTure(ta))
                            {
                                ta.buffManager.LoadTalentBuff(talent, talentBuff, timer, exportOnceData);
                            }
                        }
                    }
                }
                if (exportOnceData.loadBuffs.Count > 0 || exportOnceData.unloadBuffs.Count > 0)
                {
                    this.engineData.exports.Add(exportOnceData);
                }
            }
        }

    }
}
